package com.rop.znyx.activity.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rop.znyx.activity.mapper.CouponRangeMapper;
import com.rop.znyx.product.ProductFeignClient;
import com.rop.znyx.enums.CouponRangeType;
import com.rop.znyx.model.activity.CouponInfo;
import com.rop.znyx.activity.mapper.CouponInfoMapper;
import com.rop.znyx.activity.service.CouponInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rop.znyx.model.activity.CouponRange;
import com.rop.znyx.model.product.Category;
import com.rop.znyx.model.product.SkuInfo;
import com.rop.znyx.vo.activity.CouponRuleVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 * 优惠券信息 服务实现类
 * </p>
 *
 * @author rop
 * @since 2023-07-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {

    private final CouponRangeMapper couponRangeMapper;

    private final ProductFeignClient productFeignClient;

    /**
     * 获取分页列表
     */
    @Override
    public IPage<CouponInfo> selectPage(Page<CouponInfo> pageParam) {

        // 查询
        QueryWrapper<CouponInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        IPage<CouponInfo> page = baseMapper.selectPage(pageParam, queryWrapper);

        // 枚举转换字符串
        page.getRecords().forEach(this::convertEnumToString);

        // 返回
        return page;
    }

    /**
     * 获取优惠券
     */
    @Override
    public CouponInfo getCouponInfo(String id) {
        // 查询
        CouponInfo couponInfo = this.getById(id);

        // 枚举转换字符串
        this.convertEnumToString(couponInfo);

        // 返回
        return couponInfo;
    }

    @Override
    public Map<String, Object> findCouponRuleList(Long couponId) {
        Map<String, Object> result = new HashMap<>();
        CouponInfo couponInfo = this.getById(couponId);

        // 查询优惠券范围表
        LambdaQueryWrapper<CouponRange> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CouponRange::getCouponId, couponId);
        List<CouponRange> couponRanges = couponRangeMapper.selectList(queryWrapper);

        List<Long> rangeIdList = couponRanges.stream().map(CouponRange::getRangeId).collect(toList());

        if(CollUtil.isNotEmpty(rangeIdList)) {
            if (ObjectUtil.equal(couponInfo.getRangeType(), CouponRangeType.SKU)) {
                List<SkuInfo> skuInfoList = productFeignClient.findSkuInfoList(rangeIdList).getData();
                result.put("skuInfoList", skuInfoList);

            } else if (ObjectUtil.equal(couponInfo.getRangeType(), CouponRangeType.CATEGORY)) {
                List<Category> categoryList = productFeignClient.findCategoryList(rangeIdList).getData();
                result.put("categoryList", categoryList);

            }
        }
        return result;
    }

    @Override
    public void saveCouponRule(CouponRuleVo couponRuleVo) {

    }

    @Override
    public List<CouponInfo> findCouponByKeyword(String keyword) {
        return null;
    }

    @Override
    public List<CouponInfo> findCouponInfo(Long skuId, Long userId) {
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId).getData();
        if(null == skuInfo) {
            return new ArrayList<>();
        }
        return baseMapper.selectCouponInfoList(skuInfo.getId(), skuInfo.getCategoryId(), userId);
    }

    /**
     * 枚举转换字符串
     */
    private void convertEnumToString(CouponInfo couponInfo) {
        couponInfo.setCouponTypeString(couponInfo.getCouponType().getComment());
        if (ObjectUtil.isNotNull(couponInfo.getRangeType())) {
            couponInfo.setRangeTypeString(couponInfo.getRangeType().getComment());
        }
    }
}
